<?php 
/**
* 
*/
class General_model extends CI_Model
{
	
	function __construct()
	{
		 parent::__construct();
		 $this->load->library('session');
	}
/**
 * [get_sysparameter 获取系统参数]
 * @param  [int] 		$user 	[用户ID]
 * @param  [string] 	$key  	[系统参数关键字]
 * @return [type]       		[系统参数值]
 */
	function get_sysparameter($key,$user=0) 
	{
		$this->db->select('value');
		$this->db->from('sysset');
		$this->db->where('authorizationid', $user);
		$this->db->where('key', $key);
		$query=$this->db->get()->row_array();
		return $query['value'];
	}

	function get_filed_from_table($field,$table,$key,$value) //获取指定条件下的单个字段值
	{
		$this->db->select($field);
		$this->db->from($table);
		$this->db->where($key, $value);
		$query=$this->db->get()->row_array();
		//log_message('debug', $this->db->last_query());
		return $query[$field];
	}

	function get_all_filed_from_table($table,$key,$value,$sort="ASC") //获取指定条件下的所有值
	{
		$this->db->select('*');
		$this->db->from($table);
		$this->db->where($key,$value);
		$this->db->order_by('id', $sort);
		$query  = $this->db->get()->result_array();
		//log_message('debug', $this->db->last_query());
		return $query;
	}

	function get_count_tabel($table,$key,$value) //获取指定条件下的记录条数
	{
		$this->db->select('count(id) as num');
		$this->db->from($table);
		$this->db->where($key,$value);
		$query=$this->db->get()->row_array();
		//log_message('debug', "11111111111".$this->db->last_query());
		return $query["num"];
	}

/**
 * [get_info_wheres 获取多条件下的单个字段信息]
 * @param  [string] $table      [表]
 * @param  [string] $field      [单字段]
 * @param  [string] $wherearray [搜索条件]
 * @return [string]             [返回指定字段值]
 */
	function get_info_wheres($table,$field,$wherearray) 
	{
		$this->db->select($field);
		$this->db->from($table);
		$this->db->where($wherearray);
		$query=$this->db->get()->row_array();	
		return $query[$field];
	}
	 function get_page_info($table,$key,$value,$sort,$start_num,$per_page) //单条件带分页所有信息
    {
        $this->db->select("*");
        $this->db->from($table);
        $this->db->where($key,$value);
        $this->db->order_by("id",$sort);
        $this->db->limit($per_page,$start_num);
        $query  = $this->db->get()->result_array();
        //log_message('debug', "发现执行SQL语句->".$this->db->last_query());        
        return $query;       
    }
    function get_page_info2($table,$wherearray,$start_num,$per_page,$sort='ASC') //多条件带分页所有信息
    {
        $this->db->select("*");
        $this->db->from($table);
        $this->db->where($wherearray);
        $this->db->order_by("id",$sort);
        $this->db->limit($per_page,$start_num);
        $query  = $this->db->get()->result_array();
        //log_message('debug', "发现执行SQL语句->".$this->db->last_query());        
        return $query;       
    }
    /**
     * [get_page_infos 多条件多排序输出分页所有信息]
     * @param  [type] $table      [表名]
     * @param  [type] $wherearray [搜索条件]
     * @param  [type] $sort       [排序字符串，可多种排序]
     * @param  [type] $per_page   [步长]
     * @param  [type] $start_num  [起始]
     * @return [type]             [返回数组]
     */
    function get_page_infos($table,$wherearray,$sort,$per_page,$start_num) 
	{
		$this->db->select("*");
		$this->db->from($table);
		$this->db->where($wherearray);
		$this->db->order_by($sort);
		$this->db->limit($per_page,$start_num);
		$query=$this->db->get()->result_array();	
		log_message('debug', "发现执行SQL分页语句->".$this->db->last_query()); 	
		return $query;
	}
	/**
	 * [get_count_tabel_wherearray 获取指定多条件下的记录条数]
	 * @param  [type] $table      [表名]
	 * @param  [type] $wherearray [搜索条件]
	 * @return [type]             [返回记录数]
	 */
	function get_count_tabel_wherearray($table,$wherearray) //获取指定多条件下的记录条数
	{
		$this->db->select('count(id) as num');
		$this->db->from($table);
		$this->db->where($wherearray);
		$query=$this->db->get()->row_array();
		//log_message('debug', $this->db->last_query());
		return $query["num"];
	}
	function update_tabel_wherearray($table,$setarray,$wherearray)
	{
		$this->db->where($wherearray);
		$this->db->update($table, $setarray);
		//log_message('debug', "发现执行SQL语句->".$this->db->last_query());  
	}
	function get_info_tabel_wherearray($table,$wherearray) //获取指定多条件下的记录详情不带排序
	{
		$this->db->select('*');
		$this->db->from($table);
		$this->db->where($wherearray);
		$query=$this->db->get()->result_array();
		//log_message('debug', $this->db->last_query());
		return $query;
	}
	/**
	 * [get_infos_wheres 获取指定多条件&指定排序下的记录详情]
	 * @param  [type] $table      [表名]
	 * @param  [type] $field      [字段名称，也可以是COUNT或SUM]
	 * @param  [type] $wherearray [搜索条件]
	 * @param  [type] $key        [排序关键字]
	 * @param  string $sort       [排序方式默认ASC]
	 * @return [type]             [返回数组]
	 */
	function get_infos_wheres($table,$field,$wherearray,$key="id",$sort="ASC") 
	{
		$this->db->select($field);
		$this->db->from($table);
		$this->db->where($wherearray);
		$this->db->order_by($key,$sort);
		$query=$this->db->get()->result_array();	
		//log_message('debug', $this->db->last_query());		
		return $query;
	}
	function get_info_tabel_wherearray_order2($table,$field,$wherearray,$sort) //获取指定多条件下多排序规则下的记录详情带排序
	{
		$this->db->select($field);
		$this->db->from($table);
		$this->db->where($wherearray);
		$this->db->order_by($sort);
		$query=$this->db->get()->result_array();		
		return $query;
	}
	function getgroupinfo($table,$field,$wherearray,$groupstr,$sort="ASC")
	{	
		$this->db->select($field);
		$this->db->from($table);
		$this->db->where($wherearray);$this->db->group_by($groupstr);
		$this->db->order_by($groupstr, $sort);
		
		$query=$this->db->get()->result_array();		
		return $query;
	}
	function special_sql($sql)//特殊查询
	{
		$query = $this->db->query($sql);
		$query=	$query->result_array();		
		return $query;
	}
	function special_sql_num($sql)//特殊查询记录数
	{
		$query = $this->db->query($sql);		
		return $query->num_rows();;
	}
	function save($table,$data=array())//普通保存模型（带ID情况下更新反之插入）
    {
        if($data['id'])
        {
            $this->db->where('id',$data['id']);
            return $this->db->update($table,$data);
        }
        return $this->db->insert($table,$data);
    }

   //管理员登陆
	function enterprise_login($table,$username, $password)
	{	
		$this->db->select('*');
		$this->db->from($table);
	    $this->db->where('user',strtoupper($username) );	
		$this->db->where('password', strtoupper($password));
		$this->db->where('state', 1);
		$this->db->limit(1);
		$result = $this->db->get()->row_array();
		if (sizeof($result) > 0){return true;}	
		else{return false;}
	}
	
	function checkauth($table,$user){ //检查权限
		$this->db->select("count(id) as num");
		$this->db->from($table);
		$this->db->where('user',strtoupper($user) );
		$query=$this->db->get()->row_array();	
		if ($query["num"]==1) {
			$this->db->select("role");
			$this->db->from($table);
			$this->db->where('user',strtoupper($user) );
			$query=$this->db->get()->row_array();	
			return $query["role"];
		}else{
			return false;
		}
	}

	function enterprise_access()
	{
		if (isset( $this->session->username)) {
			$account['username'] = $_SESSION['username'];
			$this->db->select('*');
			$this->db->where('user', $account['username']);
			$this->db->where('state', 1);
			$this->db->limit(1);
			$result = $this->db->get('admin');
			$result = $result->row();
			log_message('debug', "11111111111".$this->db->last_query());
			if (!$result || is_array($result))
			{
				redirect('admin/login2');
				exit;
			}
		}else{
			redirect('admin/login1');
			exit;	
		}
	}

}

 ?>